﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;

public partial class UserProfile : ICPGeneralPage
{
    public UserData m_ud;

    #region Properties
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: User Profile";
        if (!IsPostBack && !IsCallback)
        {
            pnlUserTitle.InnerHtml = InitUserProfile();
        }
    }
    protected void cbkUserProfile_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = Handle_UserProfile(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    private String InitUserProfile()
    {
        Cached.ICPUserTable ccUser = new Cached.ICPUserTable();
        m_ud = ccUser.GetUserData(m_sxUID);

        String sxOutput = String.Empty;

        sxOutput = String.Format(
            "<img src=\"upload/avatar/{0}.jpg\" alt=\"change profile picture\" onclick=\"changeProfilePicture();\" onerror=\"setEmptyUserAvatar(this);\" class=\"cpP\" style=\"cursor:pointer;\" />" + 
            "<span id=\"cp_Header\">{1}. ID = {0}</span>",
            m_sxUID, m_sxUserName);
        return sxOutput;
    }

    private String Handle_UserProfile(String sxParameter)
    {
        String sxReturn = String.Empty;

        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        Cached.ICPUserTable ccUser = new Cached.ICPUserTable();
        m_ud = new UserData();
        m_ud.ID = m_sxUID;

        switch (sxSplitted[0].ToUpper())
        {
            case "EDIT":
                m_ud = ccUser.GetUserData(m_sxUID, true);
                sxReturn = m_ud.BuildEditor_UserProfile();
                break;
            case "SAVE":
                UpdateUserProfile(sxSplitted);
                m_ud = ccUser.GetUserData(m_sxUID, true);
                sxReturn = m_ud.BuildFieldset_UserProfile();
                break;
            case "DISCARD":
                m_ud = ccUser.GetUserData(m_sxUID);
                sxReturn = m_ud.BuildFieldset_UserProfile();
                break;
            case "ADD":
                int iLastControlId = int.Parse(sxSplitted[1]);
                String sxControlId = m_sxUID + "_" + (++iLastControlId).ToString();
                sxReturn = CustomControls.BuildEditor_ComboBoxAndText(sxControlId, KLOV.UserProfile.USERDATATYPE, 
                    KControl.INPUT_ID, null, null, null, TextboxType.FreeText);
                break;
            case "PROFPIC_POPUP":
                sxReturn = UserProfileManager.BuildEditor_ProfilePicturePopup(m_sxUID);
                break;
        }

        return sxReturn;
    }

    private void UpdateUserProfile(String[] sxSplitted)
    {
        for (int i = 1; i < sxSplitted.Length; i++) // START FROM INDEX 1
        {
            String[] sxParsed = sxSplitted[i].Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            if (sxParsed.Length > 2)
            {
                m_ud.AddCustomData(sxParsed[1], sxParsed[2]);
            }
        }

        DBQuery.ICPUserTable.UpdateCustomData(m_ud);
    }

    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    #endregion Custom Methods

}
